package cn.com.checne.controller.superManager;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.com.checne.util.PropertiesUtil;
import cn.com.checne.util.TimeUtils;

public class InitMethodController {
	private static ExecutorService executor = Executors
			.newScheduledThreadPool(1);
	private static final SimpleDateFormat sdf = new SimpleDateFormat("HH");
	private static final long MS_1HOUR = 1000*60*60 ;
	private static final String AM_1 = "01" ;
	

	public void initMethod() throws InterruptedException {
		try {
			executor.submit(new Runnable() {
				public void run() {
					try {
						String currentTime = "";
						while (true) {
							currentTime = InitMethodController.sdf
									.format(new Date());
							System.out.println(String.format(
									"[Pull News] currentTime is %s",
									new Object[] { currentTime }));
							if (currentTime.equals(AM_1)) {
								String date = TimeUtils.getDate(new Date(), -1);
								String u = PropertiesUtil.getProjectBaseURL()
										+ "/super/news/news-checNews.do?date="
										+ date;
								URL url = new URL(u);
								HttpURLConnection conn = (HttpURLConnection) url
										.openConnection();
								String responseMsg = conn.getResponseMessage();
								int responseCode = conn.getResponseCode();
								System.out.println(String
										.format("[Pull News] %s result is %s,responseCode=%d",
												new Object[] {
														u,
														responseMsg,
														Integer.valueOf(responseCode) }));
								conn.disconnect();
							}
							Thread.sleep(MS_1HOUR);
						}
					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			});
		} catch (Exception e) {
			System.err.println("[pullChecNews ExecutorService error !]" + e);
		}
	}
}